package com.ruoyi.system.service.impl;
 
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.system.mapper.AppUserMapper;
import com.ruoyi.system.service.IAppUserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
 
import javax.annotation.Resource;
import java.util.List;

/**
 * 用户 业务层处理
 *
 * @author ruoyi
 */
@Service
public class AppUserServiceImpl implements IAppUserService {
    @Resource
    private AppUserMapper appUserMapper;
 
    /**
     * 通过用户名查询用户
     *
     * @param userName 用户名
     * @return 用户对象信息
     */
    @Override
    public AppUser selectAppUserByUserName(String userName) {
        return appUserMapper.selectAppUserByUserName(userName);
    }


    @Override
    public AppUser selectAppUserByUserId(Long userId)
    {
        return appUserMapper.selectAppUserByUserId(userId);
    }

    /**
     * 查询用户信息列表
     *
     * @param appUser 用户信息
     * @return 用户信息
     */
    @Override
    public List<AppUser> selectAppUserList(AppUser appUser)
    {
        return appUserMapper.selectAppUserList(appUser);
    }

    /**
     * 新增用户信息
     *
     * @param appUser 用户信息
     * @return 结果
     */
    @Override
    public int insertAppUser(AppUser appUser)
    {
        appUser.setCreateTime(DateUtils.getNowDate());
        return appUserMapper.insertAppUser(appUser);
    }

    /**
     * 修改用户信息
     *
     * @param appUser 用户信息
     * @return 结果
     */
    @Override
    public int updateAppUser(AppUser appUser)
    {
        appUser.setUpdateTime(DateUtils.getNowDate());
        return appUserMapper.updateAppUser(appUser);
    }

    /**
     * 批量删除用户信息
     *
     * @param userIds 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteAppUserByUserIds(Long[] userIds)
    {
        return appUserMapper.deleteAppUserByUserIds(userIds);
    }

    /**
     * 删除用户信息信息
     *
     * @param userId 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteAppUserByUserId(Long userId)
    {
        return appUserMapper.deleteAppUserByUserId(userId);
    }
 
    /**
     * 密码校验
     *
     * @param password 明文
     * @param hashPwd  密文
     * @return boolean
     */
    public boolean checkPassword(String password, String hashPwd) {
        String hashPwd2 = Md5Utils.hash(password);
        return hashPwd.equals(hashPwd2);
    }

    @Override
    public boolean checkPhoneUnique(AppUser user) {
        return appUserMapper.selectAppUserByMobile(user.getMobile()) == null;
    }

    @Override
    public boolean checkEmailUnique(AppUser user) {
        return appUserMapper.selectAppUserByEmail(user.getEmail()) == null;
    }

}